package fictional.GUI;

import fictional.Common.CharacterItem;
import fictional.Common.IBL;
import fictional.Common.Response;
import fictional.Common.User;

import java.io.IOException;

/**
 * This task logs the user into the system.
 */
public class LoginTask extends DefaultTask
{
	private String userName;
	private String password;
	private Login login;
	private CharacterItem character;
	IBL BL;
    
    public LoginTask(org.jdesktop.application.Application app)
    {
        super(app);
    }

    public void setTask(IBL BL, String userName, String password, Login login)
    {
        this.userName = userName;
        this.password = password;
        this.login = login;
        this.BL = BL;
    }

    @Override
    protected Object doInBackground() throws IOException, InterruptedException
    {
        response = new Response();
    
        User user = new User(userName, password);
        boolean isSuccessful = BL.login(user, response);
        
        if (!response.isIs_Success())
        {
        	errorTitle = "Error Logging In";
        }
        else
        {
            if (isSuccessful)
            {
                character = BL.getDefaultCharacter(response);
                GUI gui = new GUI(character, login, BL);
                login.setGUI(gui);
            }
            else
            {
            	response.setIs_Success(false);
            	errorTitle = "Error Logging In";
            	response.setMessage("The username/password combination you have entered is not valid.");
            }
        }
        
        return null;
    }
}
